<?php
namespace app\mxty\model;
use think\Model;
use app\common\model\UploadFile;
use app\mxty\model\Coach;

class Course extends Model
{
    protected $table = 'athlon_course';
    protected $pk = 'course_id';

    public function image()
    {
        return $this->hasOne(UploadFile::class, 'file_id', 'image_id');
    }

    public function coachs()
    {
        return $this->belongsToMany(
            Coach::class,
            CourseCoach::class,
            'employee_id',
            'course_id'
        );
    }

    public function getAll()
    {
        return $this->with(['image', 'coachs'])
            ->where('status', 1)
            ->order('sort', 'asc')
            ->select();
    }

    public function getRecommends()
    {
        return self::with(['image', 'coachs'])
            ->where('status', 1)
            ->where('recommend', 1)
            ->select();
    }

    public function findCourse($courseId)
    {
      return $this->with(['image', 'coachs', 'coachs.image'])->where('course_id', $courseId)->where('status', 1)->findOrEmpty();
    }

    // 根据多个课程id查找课程集合
    public function getCourses($courseIds) 
    {
        return $this->where('course_id', 'in', $courseIds)->select();
    }
    
}
